package com.lk.ii.controller;

import java.util.List;

import com.lk.framework.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.lk.common.annotation.Log;
import com.lk.common.enums.BusinessType;
import com.lk.ii.domain.IiCategory;
import com.lk.ii.service.IIiCategoryService;
import com.lk.common.core.controller.BaseController;
import com.lk.common.core.domain.AjaxResult;
import com.lk.common.utils.poi.ExcelUtil;
import com.lk.common.core.page.TableDataInfo;

/**
 * 种类Controller
 * 
 * @author lk
 * @date 2021-02-02
 */
@Controller
@RequestMapping("/ii/category")
public class IiCategoryController extends BaseController
{
    private String prefix = "ii/category";

    @Autowired
    private IIiCategoryService iiCategoryService;

    @RequiresPermissions("ii:category:view")
    @GetMapping()
    public String category()
    {
        return prefix + "/category";
    }

    /**
     * 查询种类列表
     */
    @RequiresPermissions("ii:category:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IiCategory iiCategory)
    {
        startPage();
        List<IiCategory> list = iiCategoryService.selectIiCategoryList(iiCategory);
        System.out.println(list);
        return getDataTable(list);
    }

    /**
     * 导出种类列表
     */
    @RequiresPermissions("ii:category:export")
    @Log(title = "种类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IiCategory iiCategory)
    {
        List<IiCategory> list = iiCategoryService.selectIiCategoryList(iiCategory);
        ExcelUtil<IiCategory> util = new ExcelUtil<IiCategory>(IiCategory.class);
        return util.exportExcel(list, "category");
    }

    /**
     * 新增种类
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存种类
     */
    @RequiresPermissions("ii:category:add")
    @Log(title = "种类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IiCategory iiCategory)
    {
        iiCategory.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(iiCategoryService.insertIiCategory(iiCategory));
    }

    /**
     * 修改种类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        IiCategory iiCategory = iiCategoryService.selectIiCategoryById(id);
        mmap.put("iiCategory", iiCategory);
        return prefix + "/edit";
    }

    /**
     * 修改保存种类
     */
    @RequiresPermissions("ii:category:edit")
    @Log(title = "种类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IiCategory iiCategory)
    {
        iiCategory.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(iiCategoryService.updateIiCategory(iiCategory));
    }

    /**
     * 删除种类
     */
    @RequiresPermissions("ii:category:remove")
    @Log(title = "种类", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(iiCategoryService.deleteIiCategoryByIds(ids));
    }


    /**
     * 自己编写
     * @param liCategory
     * @return
     */


    @GetMapping( "/select")
    public List<IiCategory> select(IiCategory liCategory){
        List<IiCategory> list = iiCategoryService.select(liCategory);
        return list;
    }

    @GetMapping( "/test")
    @ResponseBody
    public String test(Integer id){
        return "访问成功！"+ id;
    }
}
